package org.jeecg.modules.dataAlgorithmLibrary.service.impl;

import org.jeecg.modules.dataAlgorithmLibrary.entity.DataAlgorithmLibrary;
import org.jeecg.modules.dataAlgorithmLibrary.entity.DataCiteFunction;
import org.jeecg.modules.dataAlgorithmLibrary.entity.DataResultRangeSet;
import org.jeecg.modules.dataAlgorithmLibrary.mapper.DataCiteFunctionMapper;
import org.jeecg.modules.dataAlgorithmLibrary.mapper.DataResultRangeSetMapper;
import org.jeecg.modules.dataAlgorithmLibrary.mapper.DataAlgorithmLibraryMapper;
import org.jeecg.modules.dataAlgorithmLibrary.service.IDataAlgorithmLibraryService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 算法库
 * @Author: jeecg-boot
 * @Date:   2023-02-18
 * @Version: V1.0
 */
@Service
public class DataAlgorithmLibraryServiceImpl extends ServiceImpl<DataAlgorithmLibraryMapper, DataAlgorithmLibrary> implements IDataAlgorithmLibraryService {

	@Autowired
	private DataAlgorithmLibraryMapper dataAlgorithmLibraryMapper;
	@Autowired
	private DataCiteFunctionMapper dataCiteFunctionMapper;
	@Autowired
	private DataResultRangeSetMapper dataResultRangeSetMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(DataAlgorithmLibrary dataAlgorithmLibrary, List<DataCiteFunction> dataCiteFunctionList,List<DataResultRangeSet> dataResultRangeSetList) {
		dataAlgorithmLibraryMapper.insert(dataAlgorithmLibrary);
		if(dataCiteFunctionList!=null && dataCiteFunctionList.size()>0) {
			for(DataCiteFunction entity:dataCiteFunctionList) {
				//外键设置
				entity.setLibraryId(dataAlgorithmLibrary.getId());
				dataCiteFunctionMapper.insert(entity);
			}
		}
		if(dataResultRangeSetList!=null && dataResultRangeSetList.size()>0) {
			for(DataResultRangeSet entity:dataResultRangeSetList) {
				//外键设置
				entity.setLibraryId(dataAlgorithmLibrary.getId());
				dataResultRangeSetMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(DataAlgorithmLibrary dataAlgorithmLibrary,List<DataCiteFunction> dataCiteFunctionList,List<DataResultRangeSet> dataResultRangeSetList) {
		dataAlgorithmLibraryMapper.updateById(dataAlgorithmLibrary);
		
		//1.先删除子表数据
		dataCiteFunctionMapper.deleteByMainId(dataAlgorithmLibrary.getId());
		dataResultRangeSetMapper.deleteByMainId(dataAlgorithmLibrary.getId());
		
		//2.子表数据重新插入
		if(dataCiteFunctionList!=null && dataCiteFunctionList.size()>0) {
			for(DataCiteFunction entity:dataCiteFunctionList) {
				//外键设置
				entity.setLibraryId(dataAlgorithmLibrary.getId());
				dataCiteFunctionMapper.insert(entity);
			}
		}
		if(dataResultRangeSetList!=null && dataResultRangeSetList.size()>0) {
			for(DataResultRangeSet entity:dataResultRangeSetList) {
				//外键设置
				entity.setLibraryId(dataAlgorithmLibrary.getId());
				dataResultRangeSetMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		dataCiteFunctionMapper.deleteByMainId(id);
		dataResultRangeSetMapper.deleteByMainId(id);
		dataAlgorithmLibraryMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			dataCiteFunctionMapper.deleteByMainId(id.toString());
			dataResultRangeSetMapper.deleteByMainId(id.toString());
			dataAlgorithmLibraryMapper.deleteById(id);
		}
	}
	
}
